<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px;">
        <el-row>
          <el-col :span="22">
            <el-form-item label="提示文字" v-if="false">
              <el-input v-model.trim="postForm.text"/>
            </el-form-item>
            <el-form-item label="推广码背景">
              <upload-image
                bucket="qrcode"
                :max="total"
                :max-width="750"
                :quality="100"
                @onSuccess="onSuccess"
                @onRemove="onRemove"
                :file-list="postForm.image"
              />
              <span style="color: red">可以上传最多{{total}}张， 尺寸 750 x 1334 px(尺寸不能错)</span>
            </el-form-item>
            <el-form-item label="二维码宽度">
              <el-input v-model.number="width"  style="width: 300px" />
            </el-form-item>
            <el-form-item label="二维码高度">
              <el-input v-model.number="height"  style="width: 300px" />
            </el-form-item>
            <el-form-item label="二维码X轴位置">
              <el-input v-model.number="left"  style="width: 300px" />
            </el-form-item>
            <el-form-item label="二维码Y轴位置">
              <el-input v-model.number="top"  style="width: 300px" />
            </el-form-item>
            <el-form-item label="">
              <el-button @click="resetSet">恢复默认坐标</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

      <div class="drag" v-if="postForm.image && postForm.image.length >= 1" :style="dragStyle">
        <VueDragResize
          :parent-limitation="true"
          :isActive="true"
          :w="width"
          :h="height"
          :x="left"
          :y="top"
          :minw="1"
          :minh="1"
          :stickSize="12"
          :aspectRatio="true"
          v-on:resizing="resize"
          v-on:dragging="resize">
          <div class="child">
          </div>
        </VueDragResize>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import UploadImage from '@/components/Upload/UploadImage'
import api from '@/api'
import VueDragResize from 'vue-drag-resize'
export default {
  name: 'SystemQrcode',
  components: {
    Sticky,
    UploadImage,
    VueDragResize
  },
  watch: {
    // 背景处理
    postForm: {
      handler(val, old) {
        if (val.image && val.image.length >= 1) {
          // this.dragStyle.border = '1px solid red'
          const obj = val.image[0]
          console.log(obj)
          this.dragStyle.backgroundImage = `url("${obj.url}?t=${new Date().getTime()}")`
        }
      },
      deep: true
    }
  },
  data() {
    return {
      total: 1,
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false,
      // drag
      width: 135,
      height: 135,
      top: 370,
      left: 120,
      dragStyle: {
        border: '1px solid #000'
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    // this.resize()
  },
  methods: {
    resetSet() {
      // 必须要这样操作才行
      const go = () => {
        this.width = this.height = 135
        this.top = 370
        this.left = 120
      }
      let i = 10
      const x = setInterval(() => {
        i--
        if (i > 0) {
          go()
          console.log(i)
        } else {
          clearInterval(x)
        }
      }, 5)
    },
    resize(newRect) {
      this.width = newRect.width
      this.height = newRect.height
      this.top = newRect.top
      this.left = newRect.left
      // console.log(this.width, this.width);
    },
    async fetchData() {
      const { data } = await api.system.fetchQrcode()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      // 图片
      if (this.postForm.images) {
        const list = []
        this.postForm.images.forEach(x => {
          list.push({
            id: +x.id,
            // 这个没有缩略图
            url: x.path
          })
        })
        this.postForm.image = JSON.parse(JSON.stringify(list))
      } else {
        this.postForm.image = []
      }
      this.width = data.width
      this.height = data.height
      this.left = data.left
      this.top = data.top
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          // 图片
          let image = []
          if (this.postForm.image && this.postForm.image.length > 0) {
            image = this.postForm.image.map(x => x.id)
          }
          api.system.saveQrcode({
            ...this.postForm,
            image,
            width: this.width,
            height: this.height,
            left: this.left,
            top: this.top
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.image.push({
          id: data.id,
          url: data.full_name
        })
        console.log(this.postForm.image)
        // this.submitForm()
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
        this.submitForm()
      }
    }
  }
}
</script>

<style scoped>
.drag {
  width: 375px;
  height: 667px;
  /*background: #ccc;*/
  position: absolute;
  right: 100px;
  top: 60px;
  /*line-height: 0;*/
  background-repeat: no-repeat;
  background-size: cover;
}
.child {
  /*background: red;*/
  width: 100%;
  height: 100%;
  /*line-height: 0;*/
  background-image: url("./qrcode-demo.png");
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
